package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.domain.entity.SlotDO;
import com.xgimi.open.skill.datamanager.domain.mapper.SlotMapper;
import com.xgimi.open.skill.datamanager.domain.service.SlotService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【slot(槽位表)】的数据库操作Service实现
 * @createDate 2023-02-07 19:23:48
 */
@Service
public class SlotServiceImpl extends ServiceImpl<SlotMapper, SlotDO>
        implements SlotService {

    @Override
    public List<SlotDO> querySlotByNameAndVid(SlotDO slotDO) {
        LambdaQueryWrapper<SlotDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(slotDO.getSkillVid()), SlotDO::getSkillVid, slotDO.getSkillVid());
        queryWrapper.eq(!StringUtils.isEmpty(slotDO.getSlotName()), SlotDO::getSlotName, slotDO.getSlotName());
        return this.list(queryWrapper);
    }

    @Override
    public IPage<SlotDO> pageQuery(IPage<SlotDO> page, SlotDO mapperReq) {
        LambdaQueryWrapper<SlotDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(mapperReq.getSkillVid()), SlotDO::getSkillVid, mapperReq.getSkillVid());
        queryWrapper.like(!StringUtils.isEmpty(mapperReq.getSlotName()), SlotDO::getSlotName, mapperReq.getSlotName());
        queryWrapper.orderByDesc(SlotDO::getCreatedAt);
        return this.page(page, queryWrapper);
    }

    @Override
    public SlotDO queryById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<SlotDO> listBySkillVid(Long skillVid) {
        LambdaQueryWrapper<SlotDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(skillVid), SlotDO::getSkillVid, skillVid);
        return this.list(wrapper);
    }

    @Override
    public boolean updateByIdAndVersion(SlotDO slotDO) {
        LambdaUpdateWrapper<SlotDO> updateWrapper = new LambdaUpdateWrapper<>();
        Integer updateVersion = slotDO.getUpdateVersion();
        updateWrapper.eq(SlotDO::getUpdateVersion, updateVersion);
        updateWrapper.eq(SlotDO::getId, slotDO.getId());
        // 版本自增
        slotDO.setUpdateVersion(updateVersion + 1);
        return this.update(slotDO, updateWrapper);
    }

    /**
     * 批量查询槽位dto
     * @param slotNames：槽位名称列表
     * @param skillVid：技能版本id
     * @return
     */
    @Override
    public List<SlotDO> listBySlotNames(List<String> slotNames, Long skillVid) {
        LambdaQueryWrapper<SlotDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(skillVid), SlotDO::getSkillVid, skillVid);
        wrapper.in(!CollectionUtils.isEmpty(slotNames), SlotDO::getSlotName, slotNames);
        return this.list(wrapper);
    }

    @Override
    public boolean removeBySkillVids(List<Long> skillVids) {
        if (CollectionUtils.isEmpty(skillVids)) {
            return true;
        }
        LambdaQueryWrapper<SlotDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SlotDO::getSkillVid, skillVids);
        return this.remove(queryWrapper);
    }

    @Override
    public List<SlotDO> queryByRelyonWordStock(Long wordStockId) {
        if (Objects.isNull(wordStockId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<SlotDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SlotDO::getRelyonWordStock, wordStockId);
        return this.list(queryWrapper);
    }

    @Override
    public Boolean physicalDeleteBySkillVid(Long skillVid) {
        return this.baseMapper.physicalDeleteBySkillVid(skillVid);
    }

    @Override
    public List<SlotDO> queryByNameByVidAndNoId(SlotDO slotDO) {
        LambdaQueryWrapper<SlotDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SlotDO::getSkillVid, slotDO.getSkillVid());
        queryWrapper.eq(SlotDO::getSlotName, slotDO.getSlotName());
        queryWrapper.ne(SlotDO::getId, slotDO.getId());
        return this.list(queryWrapper);
    }

}




